<template>
  <el-dialog
    :visible.sync="isShow"
    :title="mode === 'add' ? '新增部门' : '编辑部门'"
    @close="closeEvent"
  >
    <el-form ref="form" label-width="100px" :model="form" :rules="rules">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="form.name" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="form.code" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="form.manager" @focus="focusEvent">
          <el-option
            v-for="i in userList"
            :key="i.id"
            :value="i.username"
            :label="i.username"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="form.introduce" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div style="text-align: center">
        <el-button @click="isShow = false">取消</el-button>
        <el-button type="primary" @click="submit">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import { sysUserSimple, companyDepartmentPost } from '@/api/departments'
export default {
  name: 'DigItem',
  props: {
    initList: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      mode: 'add', // 区分编辑或者新增
      isShow: false,
      userList: [],
      //   接收当前点击的部门数据
      item: '',
      form: {
        name: '',
        code: '',
        introduce: '',
        manager: '',
        pid: ''
      },
      rules: {
        name: [
          { required: true, message: '必填', trigger: 'change' },
          {
            validator: (rule, value, callback) => {
              let temp = ''
              if (this.mode === 'add') {
                // 新增 找出子级部门
                temp = this.initList.filter((item) => {
                  return item.pid === this.item.id
                })
              } else {
                // 编辑 找出同级部门 排除自己
                temp = this.initList.filter((item) => {
                  return item.pid === this.item.pid && item.id !== this.item.id
                })
              }
              temp.some((item) => {
                return item.name === value.trim()
              })
                ? callback(new Error('请不要输入重复的部门名字:' + value))
                : callback()
            },
            trigger: 'change'
          }
        ],
        code: [
          { required: true, message: '必填', trigger: 'change' },
          {
            validator: (rule, value, callback) => {
              if (this.mode === 'add') {
                // 新增
                this.initList.some((item) => {
                  return item.code === value.trim()
                })
                  ? callback(new Error('请不要输入重复的部门编码:' + value))
                  : callback()
              } else {
                // 编辑
                this.initList.some((item) => {
                  return item.code === value.trim() && item.id !== this.item.id
                })
                  ? callback(new Error('请不要输入重复的部门编码:' + value))
                  : callback()
              }
            },
            trigger: 'change'
          }
        ],
        introduce: [{ required: true, message: '必填', trigger: 'change' }],
        manager: [{ required: true, message: '必填', trigger: 'change' }]
      }
    }
  },
  mounted() {
    this.$bus.$on('show', (bol, item, mode = 'add') => {
      this.isShow = bol
      this.item = item
      this.mode = mode
      if (mode === 'edit') {
        this.form = JSON.parse(JSON.stringify(item))
      }
    })
  },
  beforeDestroy() {
    this.$bus.$off(['show', 'data'])
  },
  methods: {
    async focusEvent() {
      if (this.userList.length === 0) {
        const res = await sysUserSimple()
        this.userList = res.data
      }
    },
    // 确定点击
    submit() {
      this.$refs.form.validate(async(result) => {
        if (result) {
          if (this.mode === 'add') {
            this.form.pid = this.item.id
            await companyDepartmentPost(this.form)
          } else {
            await companyDepartmentPost(this.form, this.item.id)
          }
          this.isShow = false
          this.$emit('getData')
          this.$message.success(this.mode === 'add' ? '新增成功' : '编辑成功')
        }
      })
    },
    // dialog关闭时
    closeEvent() {
      this.form = {
        name: '',
        code: '',
        introduce: '',
        manager: '',
        pid: ''
      }
      this.$refs.form.resetFields()
    }
  }
}
</script>

<style></style>
